/**
 * This file is part of the CAKIS project.
 * 
 * Copyright (c) 2009-2010 - Florian Arndt, Christian Kerl
 * 
 * @license GNU Lesser General Public License v3 <http://www.opensource.org/licenses/lgpl-3.0.html>
 * @since   04.05.2010
 * @version SVN: $Id: ProductListForm.java 111 2010-05-07 10:50:22Z f.aarndt@web.de $
 */

package org.cakis.client.forms;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;

import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.StringItem;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotFoundException;

import org.cakis.client.CakisMIDlet;
import org.cakis.client.commands.BarcodeOfflineScanCommand;
import org.cakis.client.commands.ExitCommand;
import org.cakis.client.commands.ResumeCommand;
import org.cakis.client.commands.RuntimeOptionFormCommand;
import org.cakis.client.commands.SaveProductListCommand;
import org.cakis.webservice.Product;
import org.cakis.webservice.ProductListEntry;

/**
 * ProductListForm
 *
 * @author Florian Arndt
 */
public class ProductListForm extends AbstractList {
	private RecordStore productListStore;
	private ProductListEntry[] productEntries;
	private boolean choice[];
	
	public ProductListForm(CakisMIDlet midlet) {	
		super("Aktuelle Einkaufsliste:", List.MULTIPLE, midlet);
		try {
			productListStore = 
				RecordStore.openRecordStore("productListStore", false);

			choice = new boolean[productListStore.getNumRecords()];
			productEntries = new ProductListEntry[productListStore.getNumRecords()];
			for (int i = 0; i < productListStore.getNumRecords(); i++) {
				DataInputStream input = new DataInputStream(
						new ByteArrayInputStream(productListStore.getRecord(i+1)));
				System.out.println(productListStore.getNumRecords());
				try {
					productEntries[i] = new ProductListEntry();
					productEntries[i].setProduct(new Product());
					productEntries[i].getProduct().setName(input.readUTF());
					productEntries[i].setCount(new Integer(input.read()));
					choice[i] = input.readBoolean();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} catch (RecordStoreNotFoundException e) {
			StringItem emptyMessage = new StringItem("Fehler:", 
					"Keine Einkaufsliste gespeichert!");
			Form emptyForm = new Form("Speicherfehler");
			emptyForm.append(emptyMessage);
			emptyForm.addCommand(new ExitCommand(midlet));
			emptyForm.addCommand(new RuntimeOptionFormCommand(midlet));
			emptyForm.addCommand(new BarcodeOfflineScanCommand(midlet));
			emptyForm.setCommandListener(this);
			midlet.display(emptyForm);			
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		try {
			productListStore.closeRecordStore();
		} catch (Exception e) {
			e.printStackTrace();
		}

		for (int i = 0; i < productEntries.length; i++) {
			this.append(productEntries[i].getProduct().getName() + "  x" +
					productEntries[i].getCount(), null);
		}
		this.setSelectedFlags(choice);
		
		this.addCommand(new RuntimeOptionFormCommand(midlet));
		this.addCommand(new BarcodeOfflineScanCommand(midlet));
		this.addCommand(new SaveProductListCommand(this));
		this.addCommand(new ResumeCommand(midlet));
		midlet.display(this);
	}
	
	public ProductListEntry[] getProductEntries() {
		return productEntries;
	}
}
